/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.service.base.time;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

import java.util.Objects;

/**
 * <b>(DateTimeRange)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/9/1
 */
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class DateTimeRange implements TimeRangeParser {
    /** 起始时间 */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    DateTime startTime;
    /** 截止时间 */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    DateTime endTime;

    @Override
    public String parseStartTime() {
        return Objects.nonNull(startTime) ? startTime.toString() : null;
    }

    @Override
    public String parseEndTime() {
        return Objects.nonNull(endTime) ? endTime.toString() : null;
    }

    @Override
    public String between() {
        return DateUtil.secondToTime((int) DateUtil.between(startTime, endTime, DateUnit.SECOND));
    }
}